dfs and similar dsu shortest paths trees *1900

Please click on ads to support us..

C++ Code:

// Problem: D. Design Tutorial: Inverse the Problem
// Contest: Codeforces - Codeforces Round #270
// URL: https://codeforces.com/contest/472/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<utility>
#include<set>
#include<unordered_set>
#include<list>
#include<iterator>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<bitset>
#include<random>
#include<map>
#include<unordered_map>
#include<stdio.h>
#include<complex>
#include<math.h>
#include<cstring>
#include<chrono>
#include<string>

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math") 

using namespace std;
#define pb push_back
#define ff first
#define ss second
#define vi vector <int>
#define vvi vector <vi>
#define pii pair<int, int>
#define ppi pair<int, pii>
#define vii vector<pii>
#define mii map<int, int>
#define mci map<char, int>
#define miv map<int, vi>
#define mis map<int, set<int>>
#define setbits(n) __builtin_popcount(n)
#define all(v) (v).begin(), (v).end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define endl "\n"
#define fo(i,n) for(int i=0;i<n;i++)
#define in(a,n)	for(int i=0;i<n;i++) cin>>a[i];
#define show2(a, b) cout<<a<<' '<<b<<endl
#define show3(a, b, c) cout<<a<<' '<<b<<' '<<c<<endl
#define show(arr) for (auto i:arr) cout<<i<<' '; 
#define Endl endl
const long long N=1e5+5;
const long long mod=1000000007; //998244353;

int n,cnt;
int d[2001][2001],par[2001],rnk[2001];
void make_set(int n)
{
	for(int i=1;i<=n;i++)
	{
		rnk[i]=1;par[i]=i;
	}
}
int find_set(int u)
{
	return (par[u]==u)?u:par[u]=find_set(par[u]);
}
void union_set(int u, int v)
{
	int p=par[u],q=par[v];
	if(p==q)return;
	if(rnk[p]<rnk[q])swap(p,q);
	par[q]=p;rnk[p]+=rnk[q];
}
vector<ppi> edges;
vi adj[2001];
bool dfs(int u, int p, int s, int x)
{
	if(d[s][u]!=x)return false;
	bool res=true;
	for(auto v:adj[u])
	{
		if(v==p)continue;
		res=res&dfs(v,u,s,x+d[u][v]);
	}
	return res;
}
void cases(){
	cin>>n;cnt=n;
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin>>d[i][j];
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			if(d[i][j]==0 || d[i][j]!=d[j][i])
			{
				no;return;
			}
			edges.pb({d[i][j],{i,j}});
		}
		if(d[i][i])
		{
			no;return;
		}
	}
	sort(all(edges));
	make_set(n);
	int sz=edges.size();
	for(int i=0;i<sz;i++)
	{
		int u=edges[i].ss.ff,v=edges[i].ss.ss;
		if(cnt==1)break;
		if(find_set(u)!=find_set(v))
		{
			adj[u].pb(v);adj[v].pb(u);union_set(u,v);
			cnt--;
		}
	}
	bool f=1;
	for(int i=1;i<=n;i++)
	{
		f=f&dfs(i,-1,i,0);
	}
	f?yes:no;
	
}

int32_t main(){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int t=1;
    // cin>>t;
    for (int i=0; i<t; i++){
    	//cout<<"Case #"<<i+1<<": ";
		cases();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection